package logic

import (
	"time"
	"context"
	"go-zero-all-demos/apps/user/models"
	"go-zero-all-demos/apps/user/pb/user"
	"go-zero-all-demos/apps/user/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddUserLogic {
	return &AddUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AddUser 创建用户
func (l *AddUserLogic) AddUser(in *user.UserInfo) (*user.UserID, error) {
	id := time.Now().Unix()
	err := l.svcCtx.UserModel.Insert(l.ctx, nil, &models.User{Id: id, Name: in.Name, Password: in.Password})
	if err != nil {
		return nil, err
	}
	return &user.UserID{Id: id}, nil
}
